//
//  ClubViewController.swift
//  约健
//
//  Created by tyh on 2017/9/28.
//  Copyright © 2017年 tyh. All rights reserved.
//

import UIKit

class ClubViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var clubsList:NSArray!
    @IBOutlet weak var Select: UISegmentedControl!
    @IBOutlet weak var clubtableView: UITableView!
    @IBAction func SelectChange(_ sender: UISegmentedControl) {
        
        switch Select.selectedSegmentIndex {
        case 0:
              getclubs()
        case 1:
           print("暂无！")
        default:
            break;
        }

        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "俱乐部列表"
         getclubs()
         clubtableView.delegate = self
         clubtableView.dataSource = self
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if  Select.selectedSegmentIndex == 0 && clubsList != nil{
            return clubsList!.count
        }
        else{
            return 0
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var basecell:UITableViewCell = UITableViewCell()
        if  Select.selectedSegmentIndex == 0{
        let model = clubsList[indexPath.row] as! ClubModel
        let cell = ClubListTableViewCell.creatCell(clubtableView)
            cell.hits.text = model.hits! as String
            cell.clubname.text = model.club_name! as String
            
            let imageStr = "http://172.24.10.175/workout/Uploads/" + (model.thumb! as String)
            let imageUrl = URL(string: imageStr)
            let data = try! Data(contentsOf: imageUrl!)
            let image = UIImage(data: data)
            cell.Pic.image = image
            //cell.Pic.image = UIImage(named: "GoLogin.jpg")
            basecell = cell
        }
        basecell.selectionStyle = UITableViewCellSelectionStyle.none
        return basecell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if  Select.selectedSegmentIndex == 0{
            return UIScreen.main.bounds.height * 0.25
        }else{
            return 0
        }
    }
    
    func getclubs(){
        NetWorkMngTool.sharedNetworkMngtool().Network_GetClubsListView(self.view) { (clubList:NSArray) in
            print(clubList)
            self.clubsList = clubList
            self.clubtableView.reloadData()
          
            
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let currentclub:ClubModel = clubsList[(indexPath as NSIndexPath).row] as! ClubModel
        self.performSegue(withIdentifier: "ToClubDetail", sender: currentclub)
    }
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let ClubDetailController:ClubDetailTableViewController = segue.destination  as! ClubDetailTableViewController
        ClubDetailController.Club = sender as! ClubModel
    }
    
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
